#!/local/tools/repository/php-4.4.4/bin/php
<?php

require_once "./excel.php";

function buildXls($arg)
{
  $array=array();
  $basedir="/tmp/";
  $InFilevalue=$arg."_2.txt";
  $OutFilevalue=$arg.".xls";

  $handle = fopen($basedir . $InFilevalue, "r");
  while (($dataCsv = fgetcsv($handle, 1000 , ";")) !== FALSE)
    {
      $n="";
      if($dataCsv[6] == "KO")
	{
	  $n="Unavailable";
	}
      else
	{
	  $n=$dataCsv[6];
	}

      $datetime=split(" ",$dataCsv[3]);
      $array[]=array("From" => $dataCsv[0], "CC" => $dataCsv[1], "Subject" => $dataCsv[2], "Date" => $datetime[0],"Time" => $datetime[1],"IMEI" => $dataCsv[4], "N" => $n,"NS" => $dataCsv[7],"SP" => $dataCsv[8],"RC" => $dataCsv[11]);
      //$array[]=array("IMEI" => $dataCsv[0]);
    }
  fclose($handle);

  $export_file = "xlsfile://tmp/" .$OutFilevalue;
  $fp = fopen($export_file, "wb");
  if (!is_resource($fp))
    {
      die("Cannot open $export_file");
    }
  
  $assoc=$array;
  
  fwrite($fp, serialize($assoc));
  fclose($fp);
  
  return 1;
}

//print_r($argv);

//buildXls("2007121210000424");
buildXls($argv[1]);

?>
